package com.luboyan.CBillbill.controller;

import com.luboyan.CBillbill.dao.file.Video;
import com.luboyan.CBillbill.dao.user.UserInfo;
import com.luboyan.CBillbill.service.elasticsearch.ElasticSearchService;
import com.luboyan.CBillbill.util.FastDFSUtil;
import com.luboyan.CBillbill.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
public class TestController {

    @Autowired
    private FastDFSUtil fastDFSUtil;

    @Autowired
    private ElasticSearchService elasticSearchService;

    @GetMapping("/shards")
    public void shards(MultipartFile file) throws Exception{
        fastDFSUtil.convertFile(file);
    }

    @DeleteMapping("/files-delete")
    public void delete(String path){
        fastDFSUtil.deleteFile(path);
    }

    @GetMapping("/es-videos")
    public Result<Video> getEsVideo(@RequestParam String key){
        Video videos = elasticSearchService.getVideos(key);
        return new Result<>(videos);
    }

    @DeleteMapping("/es-videos-deleteAll")
    public Result<String> deleteAllVideos(){
        elasticSearchService.deleteAllVideos();
        return Result.success();
    }

    @PostMapping("/es-userInfos")
    public Result<String> addUserInfo(@RequestBody UserInfo userInfo){
        elasticSearchService.addUserInfo(userInfo);
        return Result.success();
    }

    @DeleteMapping("/es-userInfos")
    public Result<String> deleteAllUserInfo(){
        elasticSearchService.deleteAllUserInfos();
        return Result.success();
    }

    @GetMapping("/contents")
    public Result<List<Map<String, Object>>> getContents(@RequestParam String key,
                                      @RequestParam Integer page,
                                      @RequestParam Integer pageSize) throws IOException {
        List<Map<String, Object>> contents = elasticSearchService.getContents(key, page, pageSize);
        return new Result<>(contents);
    }
}
